package com.general.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.general.template.pojo.PowerDimension;
import com.general.template.pojo.PowerUser;
import com.general.template.utils.Pages;
import com.general.template.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.general.template.pojo.PowerMeasurementScale;
import com.general.template.mapper.PowerMeasurementScaleMapper;
import com.general.template.service.PowerMeasurementScaleService;

/**
 * @Author: 杰哥
 * Date: 2021/2/16 19:15
 * @Description:
 **/

@Service
public class PowerMeasurementScaleServiceImpl extends ServiceImpl<PowerMeasurementScaleMapper, PowerMeasurementScale> implements PowerMeasurementScaleService {

    @Override
    public Pages<PowerMeasurementScale> pageList(Pages<PowerMeasurementScale> pages) {
        PowerMeasurementScaleMapper mapper = getBaseMapper();
        IPage<PowerMeasurementScale> page = new Page<>(pages.getCurrentPage(), pages.getPageSize());
        QueryWrapper<PowerMeasurementScale> wrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : pages.getParams().entrySet()) {
            if (!StringUtils.isNullOrEmpty(entry.getValue())){
                wrapper.eq(StringUtils.upperCharToUnderLine(entry.getKey()),entry.getValue());
            }
        }
        String desc = "desc";
        if (desc.equals(pages.getSortMethod())){
            wrapper.orderByDesc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
        }else{
            wrapper.orderByAsc(StringUtils.upperCharToUnderLine(pages.getSortColumn()));
        }
        wrapper.orderByDesc("jobs_id", "id");
        IPage<PowerMeasurementScale> selectPage = mapper.selectPage(page, wrapper);
        pages.setList(selectPage.getRecords());
        pages.setTotalCount((int) selectPage.getTotal());
        pages.setTotalPage((int) selectPage.getPages());
        return pages;
    }

    @Override
    public List<PowerMeasurementScale> getList(Integer jobId) {
        PowerMeasurementScaleMapper mapper = getBaseMapper();
        QueryWrapper<PowerMeasurementScale> wrapper = new QueryWrapper<>();
        wrapper.eq("jobs_id",jobId);
        return mapper.selectList(wrapper);
    }
}

